<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

export default {
  data() {
    return {
      chartDatas: [],
    }
  },
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '150px'
    },
    legend: {
      type: Boolean,
      default: false
    },
    titleShow: {
      type: Boolean,
      default: false
    },
    radius: {
      type: Array,
      default: () => {
        return ['40%', '60%']
      },
    },
    chartData: {
      type: Array,
      default: () => {
        return []
      },
    },
  },
  watch: {
    chartData: {
      deep: true,
      handler(v) {
        this.chartDatas = v.map(item => item)
        this.initChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions() {
      console.log('999', this.chartDatas);
      this.chart.setOption({
        title: {
          show: this.titleShow,
          text: '会员等级分布',
          textStyle: {
            color: '#000',
            fontWeight: 700,
            fontSize: '25px'
          }
        },
        tooltip: {
          // show: false,
          trigger: 'item'
        },
        legend: {
          show: this.legend,
          width: '70%',
          left: 'center',
          bottom: 20,
        },
        series: [
          {
            // name: 'Access From',
            type: 'pie',
            bottom: 10,
            radius: this.radius,
            center: ['50%', '50%'],
            // adjust the start and end angle
            startAngle: 0,
            endAngle: 360,
            data: this.chartDatas,
            label: {
              rotate: 'tangential',
              // formatter: '{d}%\n{b}',
              formatter: '{@value}\n{b}',
            }
          }
        ]
      })
    }
  }
}
</script>

<style></style>